package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.MallDrawActivitySaveRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.dto.draw.activity.MallDrawActivityDTO;
import com.sc.nft.entity.vo.PageAdminEquityPropsVO;
import com.sc.nft.entity.vo.draw.activity.MallDrawActivityBaseVO;
import com.sc.nft.entity.vo.draw.activity.MallDrawActivityDetailVO;
import com.sc.nft.service.EquityPropsService;
import com.sc.nft.service.MallDrawActivityBaseService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/draw/activity")
@Api(tags = "抽奖活动模块")
public class DrawActivityController {

    private final MallDrawActivityBaseService mallDrawActivityBaseService;
    
    private final EquityPropsService equityPropsService;

    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "活动列表", notes = "活动列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<MallDrawActivityBaseVO> page(@Valid @RequestBody PageRequest request) {
        return PageResult.success(mallDrawActivityBaseService.page(request.toMp()));
    }

    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑活动", notes = "编辑活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result edit(@Valid @RequestBody MallDrawActivitySaveRequest request) {
        MallDrawActivityDTO mallDrawActivityDTO = new MallDrawActivityDTO();
        try {
            BeanUtils.copyProperties(mallDrawActivityDTO,request);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return Result.success(mallDrawActivityBaseService.save(mallDrawActivityDTO));
    }

    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加活动", notes = "添加活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result add(@RequestBody MallDrawActivitySaveRequest request) {
        MallDrawActivityDTO mallDrawActivityDTO = new MallDrawActivityDTO();
        try {
            BeanUtils.copyProperties(mallDrawActivityDTO,request);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return Result.success(mallDrawActivityBaseService.save(mallDrawActivityDTO));
    }

    @PostMapping(value = "/get", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "活动详情", notes = "活动详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<MallDrawActivityDetailVO> get(@Valid @RequestBody IdRequest request) {
        MallDrawActivityDTO mallDrawActivityDTO =  mallDrawActivityBaseService.get(request.getId());
        MallDrawActivityDetailVO mallDrawActivityDetailVO = new MallDrawActivityDetailVO();
        try {
            BeanUtils.copyProperties(mallDrawActivityDetailVO,mallDrawActivityDTO);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return Result.success(mallDrawActivityDetailVO);
    }

    @PostMapping(value = "/prop/list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "道具列表", notes = "道具列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<PageAdminEquityPropsVO>> propList() {
        return PageResult.success(equityPropsService.getPageAdminEquityProps(null, true, null, 1, Integer.MAX_VALUE).getRecords());
    }
}
